package com.inossem.warehousestorage_inossem.manage;

import android.content.Context;
import android.widget.ImageView;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;
import com.inossem.utils.request.VolleyUtils;
import com.inossem.warehousestorage_inossem.R;

/**
 * Created by leij on 2017/11/29.
 */

public class ImageManage {

    private static RequestQueue requestQueue;

    private static ImageLoader imageLoader;

    private synchronized static ImageLoader getImageLoader(Context context) {
        if (requestQueue == null || imageLoader == null) {
            requestQueue = Volley.newRequestQueue(context.getApplicationContext());
            imageLoader = VolleyUtils.getImageLoader(requestQueue);
        }
        return imageLoader;
    }

    public static void requestImage(Context context, ImageView imageView, String url) {
        requestImage(context, imageView, url, -1, -1);
    }

    public static void requestImage(Context context, ImageView imageView, String url, int defaultIcon, int errorIcon) {
        ImageLoader imageLoader = getImageLoader(context);
        ImageLoader.ImageListener listener = ImageLoader.getImageListener(imageView,
                defaultIcon == -1 ? R.drawable.loading_default : defaultIcon, errorIcon == -1 ? R.drawable.loading_error : errorIcon);
        imageLoader.get(url, listener);
    }

}
